package araxy.blog.configuration;

import araxy.blog.entity.QuartzBean;
import araxy.blog.task.GitSyncJob;
import araxy.blog.utils.QuartzUtil;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfiguration {

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        //覆盖已存在的任务
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        //延时60秒启动定时任务，避免系统未完全启动却开始执行定时任务的情况
        schedulerFactoryBean.setStartupDelay(60);
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
        return schedulerFactoryBean.getScheduler();
    }

    @Bean
    public QuartzBean gitSyncTask() {
        QuartzBean task = new QuartzBean();
        task.setJobName("gitSync");
        task.setJobClass(GitSyncJob.class);
        //每隔一天执行一次
        task.setCronExpression("* * * */1 * ?");
        return task;
    }

}
